package edu.uoc.tdp.pac4.client.logandreg;

import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;

import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import javax.swing.JRadioButton;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JToolBar;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Registration_ViewRegStatus extends JPanel {
	

	private static final long serialVersionUID = 1L;

	private List<RegistrationData> statusRegistrations = null;
	
	// Connection to the services
	ClientConnectionHelper connectionHelper_ = null;
	private JPanel filterPanel = null;
	private JPanel resultsPanel = null;
	private JScrollPane resultsScroll = null;
	private JButton btnSearch = null;
	private JButton btnNewReg = null;
	private JTable table = null;
	
	//RadioButtons:
	private JRadioButton allRdBtn = null;
	private JRadioButton okRdBtn = null;
	private JRadioButton pdtRdBtn = null;
	private JRadioButton noOkRdBtn = null;
	
	/**
	 * Create the panel.
	 */
	public Registration_ViewRegStatus(ClientConnectionHelper connectionHelper) {
		setLayout(new BorderLayout(0, 0));
		
		connectionHelper_ = connectionHelper;
		
		//Filters
		filterPanel = new JPanel();
		filterPanel.setBorder(new TitledBorder(null, LanguageUtils.getMessage("client.regandlog.title.viewregistrations"), TitledBorder.LEADING,TitledBorder.TOP, null, null));
		add(filterPanel, BorderLayout.NORTH);
		filterPanel.setLayout(new GridLayout(1, 4, 0, 0));
		
		ButtonGroup buttonGroup = new ButtonGroup();
		
		allRdBtn = new JRadioButton(LanguageUtils.getMessage("client.regandlog.rbtnlabel.all"));
		buttonGroup.add(allRdBtn);
		filterPanel.add(allRdBtn);
	
		okRdBtn = new JRadioButton(LanguageUtils.getMessage("client.regandlog.rbtnlabel.accepted"));
		buttonGroup.add(okRdBtn);
		filterPanel.add(okRdBtn);
		
		pdtRdBtn = new JRadioButton(LanguageUtils.getMessage("client.regandlog.rbtnlabel.pending"));
		buttonGroup.add(pdtRdBtn);
		filterPanel.add(pdtRdBtn);

		noOkRdBtn = new JRadioButton(LanguageUtils.getMessage("client.regandlog.rbtnlabel.declined"));
		buttonGroup.add(noOkRdBtn);
		filterPanel.add(noOkRdBtn);

		allRdBtn.setSelected(true);
		
		//Results
		resultsPanel = new JPanel();
		add(resultsPanel, BorderLayout.CENTER);
		resultsPanel.setLayout(new BorderLayout());
		
		resultsScroll = new JScrollPane((Component) null);
		resultsPanel.add(resultsScroll, BorderLayout.CENTER);
		
		table = getRegsTable();
		resultsScroll.add(table);
		resultsScroll.setViewportView(table);
		
		//Button bar
		JToolBar toolBar = new JToolBar();
		add(toolBar, BorderLayout.SOUTH);
		
		btnSearch = new JButton(LanguageUtils.getMessage("client.button.search"));
		btnSearch.setHorizontalAlignment(SwingConstants.RIGHT);
		toolBar.add(btnSearch);	
		
		btnSearch.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if (e.getSource() == btnSearch) {
							try {	
								if(allRdBtn.isSelected()){
									statusRegistrations = new RegDataHelper().getAllRegistrationData(connectionHelper_);}
								else if(okRdBtn.isSelected())
									statusRegistrations = new RegDataHelper().getAcceptedRegistrationData(connectionHelper_);
								else if(noOkRdBtn.isSelected())
									statusRegistrations = new RegDataHelper().getDeclinedRegistrationData(connectionHelper_);
								else if(pdtRdBtn.isSelected())
									statusRegistrations = new RegDataHelper().getPendingRegistrationData(connectionHelper_);
								
								resultsScroll.setViewportView(getRegsTable());
								
							}catch(Exception e2) {
									JOptionPane.showMessageDialog(null, LanguageUtils.getMessage("client.error.message"),
											LanguageUtils.getMessage("client.error.title"),JOptionPane.ERROR_MESSAGE);
							}
				
						}
					}
				}
		);
		
		JLabel lblNewLabel = new JLabel("       ");
		toolBar.add(lblNewLabel);
		
		btnNewReg = new JButton(LanguageUtils.getMessage("client.regandlog.button.newregistration"));
		btnNewReg.setHorizontalAlignment(SwingConstants.LEFT);
		toolBar.add(btnNewReg);
		btnNewReg.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if (e.getSource() == btnNewReg) {
							try {	
								Registration_NewRegistration newRegistration = new Registration_NewRegistration(connectionHelper_);
								newRegistration.setVisible(true);
							}catch(Exception e2) {
									JOptionPane.showMessageDialog(null, LanguageUtils.getMessage("client.error.message"),
											LanguageUtils.getMessage("client.error.title"),JOptionPane.ERROR_MESSAGE);
							}
				
						}
					}
				}
		);
	}
		
	private JTable getRegsTable() {

			JTable tableStudentResults = null;
			if (tableStudentResults == null) {
				tableStudentResults = new JTable();
				StudentRegStatusModel model = new StudentRegStatusModel(statusRegistrations);
				tableStudentResults.setModel(model);
			}
			tableStudentResults.setEnabled(false);
			return tableStudentResults;
		}	
}

